<template>
  <div class="Register">
    <!-- 背景图 -->
    <div class="bgp"></div>
    <div class="Register_container">
      <div class="qiu">
        <XQiu />
      </div>
      <el-form
        class="Register_form"
        :model="userInfo"
        :rules="rules"
        @keyup.enter="onRegister"
        ref="ref_form"
      >
        <el-form-item prop="userName">
          <el-input
            placeholder="请输入3到6个任意字符串作为用户名"
            v-model.trim="userInfo.userName"
          >
            <!-- 图标 -->
            <template #prepend>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入6到15个任意字符串作为密码"
            show-password
            v-model.trim="userInfo.password"
          >
            <!-- 图标 -->
            <template #prepend>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="keyCode">
          <el-input placeholder="请输入验证码" v-model.trim="userInfo.keyCode">
            <!-- 图标 -->
            <template #prepend>
              <el-icon><Paperclip /></el-icon>
            </template>
          </el-input>
          <!-- 验证码 -->
          <div class="Register_code">
            {{ keyCode }}
            <!-- 刷新 -->
            <el-icon class="sx" @click="createCode"><RefreshLeft /></el-icon>
          </div>
        </el-form-item>
        <el-button type="success" class="Register_submit" @click="onRegister"
          >注册</el-button
        >
        <div class="Register_register" @click="toGo" style="color: #ba2f05">
          去登录
        </div>
      </el-form>
    </div>
    <ul>
      <li><img src="../assets/img/1.png" /></li>
      <li><img src="../assets/img/2.png" /></li>
      <li><img src="../assets/img/3.png" /></li>
      <li><img src="../assets/img/4.png" /></li>
    </ul>
  </div>
</template>
<script setup>
import router from '../router/index'
import xQiu from '../components/xQiu.vue'
import { reactive, ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getRegister } from '../api/index'
// 生成验证码
var keyCode = ref(null)
var createCode = () => {
  let code = ''
  let arr = [
    0,
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    'A',
    'B',
    'C',
    'D',
    'E',
    'F',
    'G',
    'H',
    'I',
    'J',
    'K',
    'L',
    'M',
    'N',
    'O',
    'P',
    'Q',
    'R',
    'S',
    'T',
    'U',
    'V',
    'W',
    'X',
    'Y',
    'Z',
  ]
  for (let i = 0; i < 4; i++) {
    let index = Math.floor(Math.random() * 36)
    code += arr[index]
  }
  keyCode.value = code
}
createCode()

// 表单数据
const userInfo = reactive({
  userName: '',
  password: '',
  keyCode: '',
})

// 表单数据校验规则
const rules = {
  userName: [{ required: 'true', trigger: 'blur', message: '用户名不能为空' }],
  password: [{ required: 'true', trigger: 'blur', message: '密码不能为空' }],
  keyCode: [{ required: 'true', trigger: 'blur', message: '验证码不能为空' }],
}

// 注册方法
const ref_form = ref(null)
const onRegister = () => {
  ref_form.value.validate((val) => {
    if (val) {
      if (userInfo.keyCode.toUpperCase() !== keyCode.value) {
        return ElMessage.error('验证码不正确！')
      }
      getRegisterData()
    }
  })
}

// 注册接口
const getRegisterData = async () => {
  // 接口的调用
  const res = await getRegister({
    name: userInfo.userName,
    pwd: userInfo.password,
  })
  console.log(res)
  // 如果注册成功
  if (res.status === 200) {
    ElMessage({
      message: '注册成功！！！',
      type: 'success',
    })
    router.push('/login')
  }
}

// 跳转登录页面
const toGo = () => {
  router.push('/login')
}

// 换背景逻辑
onMounted(() => {
  var bgp = document.querySelector('.bgp')
  var imgs = document.querySelectorAll('img')
  imgs[0].style.borderColor = '#a71d00'
  var i = 0
  setInterval(() => {
    i++
    if (i > 3) {
      i = 0
    }
    bgp.style.backgroundImage = 'url(' + imgs[i].src + ')'
    for (var a = 0; a < imgs.length; a++) {
      imgs[a].style.borderColor = '#fff'
    }
    imgs[i].style.borderColor = '#a71d00'
  }, 3000)
})
</script>
<style lang="less" scoped>
ul {
  position: absolute;
  left: 25%;
  top: 10px;
  width: 650px;
  height: 100px;
}

ul li {
  float: left;
  list-style: none;
  width: 150px;
  height: 100px;
  margin-left: 12px;
}

ul li img {
  width: 100%;
  height: 100%;
  border: 3px solid #fff;
  transition: all 2s;
}

@-webkit-keyframes fadenum {
  0% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}

.qiu {
  position: relative;
  top: -50px;
  left: -121px;
}

.Register {
  height: 100%;
  padding: 0;
  margin: 0;
  .bgp {
    height: 100%;
    background-image: url('../assets/img/1.png');
    background-repeat: no-repeat;
    background-size: 100%;
    background-position: 50% 50%;
    transition: all 2s;
  }

  .el-icon {
    height: 30px !important;
  }

  .Register_container {
    animation: fadenum 3s ease;
    position: absolute;
    z-index: 1;
    width: 400px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transition: all 1s;
    min-height: 273px;
    text-align: center;
    background-color: #fff;
    border-radius: 10px;
    padding: 20px;

    .el-card {
      border-radius: 20px;
    }

    .Register_form {
      .Register_submit {
        width: 100%;
        height: 50px;
        border-radius: 20px;
        margin-bottom: 10px;
      }

      .Register_register {
        cursor: pointer;
        width: 6.25rem;
        font-size: 0.875rem;
        margin: 0 auto;
      }

      .Register_code {
        position: absolute;
        z-index: 1;
        top: 0;
        right: 0;
        background-color: #fff;
        color: #000;
        width: 100px;
        height: 30px;
        line-height: 30px;
        font-size: 18px;
        padding-left: 10px;
        border: 1px solid #ccc;
        .sx {
          position: absolute;
          cursor: pointer;
          left: 10px;
          top: 0;
          font-size: 18px;
        }
      }

      :deep(.el-input-group__prepend) {
        padding: 0 10px;
      }
    }
  }
}
</style>
